﻿#ifndef PST_GRAPH2DSETTINGS_H
#define PST_GRAPH2DSETTINGS_H

#include <QWidget>
#include "T_BasePage.h"
#include "Def.h"
#include "PostEnumType.h"
#include "ElaLineEdit.h"
#include "InfomationCurve2DBasical.h"
#include <QPair>
#include <ElaDoubleSpinBox.h>
//#include "QVBoxLayout.

class ElaTabWidget;
class ElaListView;
class ElaComboBox;
class QGridLayout;
//class ElaLineEdit;
class ElaSpinBox;
class QStandardItem;
class ElaCheckBox;
//class QVBoxLayout;


class CustomDoubleSpinBox : public ElaDoubleSpinBox {
    Q_OBJECT
public:
    CustomDoubleSpinBox(QWidget* parent = nullptr) : ElaDoubleSpinBox(parent) {
        // 设置默认小数位数  
        setDecimals(6);
    }

protected:
    QString textFromValue(double value) const override {
        // 格式化输出  
        return QString::number(value, 'f', 6).remove(QRegExp("0+$")) // 去除末尾的 0  
            .remove(QRegExp("\\.$")); // 去除末尾的 .  
    }

    double valueFromText(const QString& text) const override {
        return text.toDouble(); // 将文本转换回 double  
    }
};

namespace pst
{

    /// @brief 二维图形的设置UI
    class Graph2DSettings : public T_BasePage
    {
        Q_OBJECT
    public:
        explicit Graph2DSettings(QWidget* parent = nullptr, Cure2DType type = Cure2DType::None);
        ~Graph2DSettings();
    public:
        /// 选择曲线列表 
        void setCurveList(const QList<QString>& curveList);
        QStandardItem* addCurveToList(const QString& curveName);
        void clearCurveList();

        /// 设置固定值和变量值列表， 名字和数字；选择固定值，剩下的就是变量值
        void setFixedVariableValue(const QList<QPair<QString, QList<QString>>>& fixedValueList);
        void setCurrentFixedValueIndex(int index);
        int getCurrentVariableValueIndex() const;
        // 只进行界面布局，不进行数据更新。
        void updateUILayout();

        void setCategaryData(const QList<QString>& categaryList);
        QList<QString> getCategaryData() const;

        //隐藏曲线属性面板 page1上除了最上面曲线名部分
        void hideCurvePropertylPanel();
        void setCurvePropertyPanelState(const InfomationCurve2DBasical& info);

        void setXAxisName(const QString& name) { _titleHEditX->setText(name); }
        QString getXAxisName()const { return _titleHEditX->text(); };

        void setYAxisName(const QString& name) { _titleHEditY->setText(name); }
        QString getYAxisName()const { return _titleHEditY->text(); };
    private:
        QString getFontFamilyString(const QString& font);

        void addTopTabPivotUI();
        void addCurveListUI();
        void addVariableAxistUI();
        void setFixedValue();
        void setVariableValue();
        void addFiexedValueToUI();
        void addCategaryToUI();
        void setCategoryData();
        void setValueColumnUI();
        void setDataOptrationUI();

        //page2
        void addXAxisSettingUI();
        void addYAxisSettingUI();

        void addCurvePropertyUI();
        void addLegendPropertyUI();
    signals:
        void signal_displayDataChanged();  //更新选择的曲线数据
        void signal_axisPropertyChanged(); //更新坐标轴属性

        void signal_XAxisTitleNameChanged(const QString& titleName);
        void signal_XAxisTitleFontFamilyChanged(const QString& fontFamily);
        void signal_XAxisTitleFontSizeChanged(int fontSize);
        void signal_XAxisMinValueChanged(double minValue);
        void signal_XAxisMaxValueChanged(double maxValue);
        void signal_XAxisStepValueChanged(double step);

        void signal_YAxisTitleNameChanged(const QString& titleName);
        void signal_YAxisTitleFontFamilyChanged(const QString& fontFamily);
        void signal_YAxisTitleFontSizeChanged(int fontSize);
        void signal_YAxisMinValueChanged(double minValue);
        void signal_YAxisMaxValueChanged(double maxValue);
        void signal_YAxisStepValueChanged(double step);

        //当前曲线修改了数据后，重新进行绘图
        void signal_updateCurveData(const QList<QStandardItem*>& itemList,
            const InfomationCurve2DBasical& changedInfo);
        // 更改了扫描参数后，产生一个新图，并添加新参数曲线， 然后重新绘图
        void signal_generateANewGraphCurve(const QList<QStandardItem*>& itemList,
            const InfomationCurve2DBasical& changedInfo);
        void signal_chosenItemsChanged(const QList<QStandardItem*>& itemList);

        void signal_legendTitleFontFamilyChanged(const QString& fontFamily);
        void signal_legendTitleFontSizeChanged(int fontSize);

        void signal_showHideCurve(const QPair<int, bool>& curveState);
    public slots:
        void slot_variableAxisChanged(int index);
        void slot_curveNameClicked();
        void slot_updateCurveData();
        void slot_checkBoxNormalizeClicked();
        void slot_checkBoxDBClicked();
        void slot_xAxisRangeChange(double minValue, double maxValue);
        void slot_yAxisRangeChange(double minValue, double maxValue);
    private:
        /// @brief 选项卡视图组件
        ElaTabWidget* m_tabWidget{ nullptr };
        /// @brief 已经添加过的曲线数目
        int m_curveCount{ 0 };

    private:
        /// =============UI组件=============
        /// 第一页内容
        QWidget* m_page1CentralWidget;
        QWidget* m_page2CentralWidget;
        QListView* m_curveNameListView;            //曲线列表视图
        //ElaListView* m_variableValueListView;        //变量值列表视图
        QGridLayout* _fixedValueLayoutH;             //固定值控件布局
        int _titleFontSize;
        int _contentWidth;
        int m_variableValueIndex;                       //当前的变量轴值index
        QList<QPair<QString, QList<QString>>> m_FixedVariableValueList; //所有数据key
        QVBoxLayout* _page1Layout;
        QList<QPair<QWidget*, QWidget*>> m_currentFixedValueWidgets;    //当前固定值区域存储的所有控件
        ElaComboBox* m_categryValue;                                     //类别下拉框
        QList<QString> m_categoryValueList;      //分类value

        QGridLayout* _page2Layout;
        int page2RowCount;

        ElaLineEdit* _titleHEditX;
        ElaComboBox* _fontFamilyComboBoxX;
        ElaSpinBox* _fontSizeInputX;
        CustomDoubleSpinBox* _minInputX;
        CustomDoubleSpinBox* _maxInputX;
        ElaComboBox* _stepComboBoxX;
        CustomDoubleSpinBox* _setpInputX;

        ElaLineEdit* _titleHEditY;
        ElaComboBox* _fontFamilyComboBoxY;
        ElaSpinBox* _fontSizeInputY;
        CustomDoubleSpinBox* _minInputY;
        CustomDoubleSpinBox* _maxInputY;
        ElaComboBox* _stepComboBoxY;
        CustomDoubleSpinBox* _setpInputY;

        //变量轴UI
        ElaText* m_variableAxisText;
        ElaComboBox* m_variableAxisValue;            //变量轴的下拉框
        QString m_lastVariableAxisText;
        bool m_isAddCurve{ false }; //用于重新将变量轴设置回去，并且不触发产生新曲线

        //固定值区域
        ElaText* _fixedValueText;
        QWidget* _fixedValueArea;

        //value UI
        ElaText* m_valueText;
        ElaComboBox* m_valueDataValue;            //变量轴的下拉框

        ElaCheckBox* _checkBoxNormalize;
        ElaCheckBox* _checkBoxDB;


        QList<ElaComboBox*> m_fixedCombo; //固定值的所有下拉框

        ElaComboBox* _legendFontFamilySelector;
        ElaComboBox* _legendFontSizeSelector;
        //当前面板显示的数据
        InfomationCurve2DBasical m_currentTempInfo;
    };
}

#endif // !PST_GRAPH2DSETTINGS_H
